package com.smile.controller.sys;

import com.smile.annotation.NoLogin;
import com.smile.entity.sys.UserConditionEntity;
import com.smile.entity.sys.UserEntity;
import com.smile.service.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smile.entity.ResponsePageEntity;

import javax.validation.constraints.NotNull;

/**
 * 用户 接口层
 * 
 * @author smile
 * @date 2025-09-19 14:22:09
 */
@Api(tags = "用户操作", description = "用户接口")
@RestController
@RequestMapping("/v1/user")
@Slf4j
public class UserController {
	
	@Autowired
	private UserService userService;

	/**
	 * 通过id查询用户信息
	 *
	 * @param id 系统ID
	 * @return 用户信息
	 */
	@PreAuthorize("hasAnyRole('User')")
	@ApiOperation(notes = "通过id查询用户信息", value = "通过id查询用户信息")
	@GetMapping("/findById")
	public UserEntity findById(Long id) {
		UserEntity userEntity = userService.findById(id);
		log.info("userEntity:{}", userEntity);
		return userEntity;
	}

	/**
    * 根据条件查询用户列表
    *
    * @param userConditionEntity 条件
    * @return 用户列表
    */
	@ApiOperation(notes = "根据条件查询用户列表", value = "根据条件查询用户列表")
	@PostMapping("/searchByPage")
	public ResponsePageEntity<UserEntity> searchByPage(@RequestBody UserConditionEntity userConditionEntity) {
		return userService.searchByPage(userConditionEntity);
	}


	/**
     * 添加用户
     *
     * @param userEntity 用户实体
     * @return 影响行数
     */
	@NoLogin
	@ApiOperation(notes = "添加用户", value = "添加用户")
	@PostMapping("/insert")
	public void insert(@RequestBody UserEntity userEntity) {
		 userService.insert(userEntity);
	}

	/**
     * 修改用户
     *
     * @param userEntity 用户实体
     * @return 影响行数
     */
	@ApiOperation(notes = "修改用户", value = "修改用户")
	@PostMapping("/update")
	public int update(@RequestBody UserEntity userEntity) {
		return userService.update(userEntity);
	}

	/**
     * 删除用户
     *
     * @param id 用户ID
     * @return 影响行数
     */
	@ApiOperation(notes = "删除用户", value = "删除用户")
	@PostMapping("/deleteById")
	public int deleteById(@RequestBody @NotNull Long id) {
		return userService.deleteById(id);
	}
}
